#include "Communication.h"
#include <QImage>

Communication::Communication(QObject* parent):
    QObject(parent)
{
    m_connector = NULL;
}

Communication::~Communication()
{
}

void Communication::connectToAibo(const QString &host, AiboState *state)
{
    m_host = host;
    m_connector = new ConnectionThread(host, state);
    m_connector->start();
}

void Communication::closeConnection()
{
	if( this->m_connector )
		this->m_connector->killConnection();
}

bool Communication::isTcpConnected()
{
	return m_connector != NULL ? m_connector->isTcpConnected() : false;
}

bool Communication::isWalkConnected()
{
	return m_connector != NULL ? m_connector->isWalkConnected() : false;
}

bool Communication::isHeadConnected()
{
	return m_connector != NULL ? m_connector->isHeadConnected() : false;
}

bool Communication::isBehaviorConnected()
{
	return m_connector != NULL ? m_connector->isBehaviorConnected() : false;
}

bool Communication::isJointMonitorConnected()
{
	return m_connector != NULL ? m_connector->isJointMonitorConnected() : false;
}

bool Communication::isCameraConnected()
{
	return m_connector != NULL ? m_connector->isCameraConnected() : false;
}

QImage Communication::getCamera()
{
    QByteArray arrayPixel = m_connector->getCurrImageData();
    QImage img = QImage(m_connector->getCameraWidth(), m_connector->getCameraHeight(), QImage::Format_RGB32);
    img.loadFromData((const uchar*)arrayPixel.constData(), arrayPixel.size(), "JPG");
    img = img.scaled(QSize(600, 400), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    return img;
}

void Communication::sendWalkCmd(char type, float amount)
{
	if( m_connector )
		m_connector->requestSendWalkCmd(type, amount);
}

void Communication::sendHeadPointCmd(char type, float amount)
{
	if( m_connector )
		m_connector->requestSendHeadCmd(type, amount);
}
